package com.example.rabbitmqextension.listener;

import com.example.rabbitmqextension.constant.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Component
public class ackListener {
    @RabbitListener(queues = Constants.ACK_QUEUE)
    public void receive(Message message, Channel channel) throws Exception {
        long deliverTag = message.getMessageProperties().getDeliveryTag();
        try {

            System.out.println("业务正在处理");
            System.out.printf("接收到消息: %s,deliverTag: %d",new String(message.getBody(),"UTF-8"),
                    deliverTag);
            System.out.println();

            //业务逻辑处理
            //int num = 3/0;
            System.out.println("业务处理完成");
            channel.basicAck(deliverTag,false); //肯定确认
        } catch (Exception e) {
            channel.basicNack(deliverTag,false,true); // 否定确认
        }

    }
}
